<?php
namespace app\admin\model;

use app\admin\validate\CheckArea;
use app\common\impl\Comm;
use app\common\model\AdminBaseModel;
use think\Exception;
use think\facade\Db;
use Jrk\Tree;


class Areas extends AdminBaseModel implements Comm
{
    //表名
    protected $name = "area";


    public function getAdminPageData($param = [], $order = 'id asc')
    {
        // TODO: Implement getAdminPageData() method.

        $where = [];
        if (isset($param['name']) && $param['name'] != '') {
            $where[] = ['name', 'like', "%" . $param['name'] . "%"];
        }

        try {
            $data = self::limit_select($where, $order);
            $count = self::hhy_count($where);

            if (!empty($data)) {
                // $data = Tree::toFormatTree($data, "name");
            }

            return parent::ajaxResult($data, $count);
        } catch (Exception $exception) {
            return parent::ajaxResult([], 0, 100, $exception->getMessage());
        }
    }


    public function delById($id)
    {
        // TODO: Implement delById() method.

        return parent::del($id);
    }


    public function doAll($data)
    {
        // TODO: Implement doAll() method.
        try {
            $check = new CheckArea();
            if (!$check->check($data)) {
                return parent::JsonReturn($check->getError(), 0);
            }

            if (isset($data['id'])) {
                $res = self::update($data);
                $id = $data['id'];
            } else {
                $res = self::save($data);
                $id = $this->id;
            }
            
            if ($res) {
                return self::JsonReturn("更新添加成功");
            } else {
                return self::JsonReturn("更新添加失败", 0);
            }
        } catch (\Exception $e) {
            return self::JsonReturn($e->getMessage(), 0);
        }
    }

    public function edit($param)
    {
        return self::update($param);
    }


}